package com.joysuch.wwyt.workflow.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.workflow.bean.*;
import com.joysuch.wwyt.workflow.entity.WorkFlowMenu;
import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.service.WorkFlowMenuService;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/workflow/menu")
@Api(tags = "业务流程-菜单配置")
@CrossOrigin
public class WorkFlowMenuController {

    @Autowired
    private WorkFlowMenuService workFlowMenuService;

    @Autowired
    private WorkFlowProcessService workFlowProcessService;


    @GetMapping("/list")
    @ApiOperation(value = "01 流程菜单配置列表", response = ProcessMenuBean.class)
    public ResultBean list(Long processId) {
           List<WorkFlowMenu> menuList = workFlowMenuService.getMenuList(processId);
           ProcessMenuBean menuBean = new ProcessMenuBean();
           menuBean.setMenuList(menuList);
           Optional<WorkFlowProcess> optionalProcess = workFlowProcessService.findById(processId);
           if(optionalProcess.isPresent()){
               WorkFlowProcess process= optionalProcess.get();
               menuBean.setType(process.getType());
               return ResultBean.success(menuBean);
           }else{
               return ResultBean.fail(Status.PROCESS_NOT_FIND_ERROR);
           }
    }

    @RequiresPermissions("workflow:menu:save")
    @PostMapping("/save")
    @ApiOperation(value = "02 保存流程菜单配置", response = WorkFlowProcessBean.class)
    public ResultBean save(@RequestBody @Validated WorkFlowMenuBean bean) {
        try {
            workFlowMenuService.saveProcessMenu(bean);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.fail(e.getCode(), e.getMessage());
        }
    }


    @GetMapping("/getById/{menuId}")
    @ApiOperation(value = "03 根据流程菜单ID获取详情", response = WorkFlowProcessBean.class)
    @ApiImplicitParam(name = "menuId", value = "流程菜单ID", paramType = "path", required = true)
    public ResultBean getById(@PathVariable Long menuId) {
        WorkFlowMenu workFlowMenu = workFlowMenuService.getById(menuId);
        return ResultBean.success(workFlowMenu);

    }

    @RequiresPermissions("workflow:menu:delete")
    @GetMapping("/delete/{menuId}")
    @ApiOperation(value = "04 根据业务流程ID删除")
    @ApiImplicitParam(name = "menuId", value = "流程菜单ID", paramType = "path", required = true)
    public ResultBean delete(@PathVariable Long menuId) {
        workFlowMenuService.deleteMenu(menuId);
        return ResultBean.defaultSuccessResult();

    }


    @GetMapping("/getTaskNameList/{processId}")
    @ApiImplicitParam(name = "processId", value = "业务流程ID", paramType = "path", required = true)
    @ApiOperation(value = "05 获取业务流程节点名称列表")
    public ResultBean getTaskNameList(@PathVariable Long processId) {
        List<WorkFlowTaskNameBean> taskList = workFlowProcessService.findTaskNameList(processId);
        return ResultBean.success(taskList);

    }


    @GetMapping("/getSystemList")
    @ApiOperation(value = "06  流程所属系统列表")
    public ResultBean getSystemList() {
        List<WorkFlowMenuSystemBean> systemList = workFlowMenuService.getSystemList();
        return ResultBean.success(systemList);

    }

    @GetMapping("produce-base-menu")
    @ApiOperation("基于某流程生成菜单")
    @Deprecated
//    @RequiresPermissions("workflow:menu:produce-base-menu")
    public ResultBean produceBaseMenu(@RequestParam("processId") Long processId) {
        return workFlowMenuService.produceBaseMenu(processId);
    }

}
